***************
** Figure 3
***************

global root "D:\Dropbox\unequal_gains\QJE revision plan\analysis\CEX"
global rootcpi "D:\Dropbox\unequal_gains\QJE revision plan\analysis\CPI"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

** Panels A & B **

use  "$rootcpi\cpi_cex_2004_2015_final", clear
rename spending spending_all
foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
bysort year: egen double tot_`i'=sum(`i')
gen s_`i'=`i'/tot_`i'
}

egen id=group(series_id)
tsset id year

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
gen so_`i'=L.s_`i'
}

* Compute price indices
gen price_ratio=infl+1

foreach i in all incq1 incq2 incq3 incq4 incq5 {

* Laspeyres & paasche
bysort year: egen laspeyres_`i'=sum(infl*so_spending_`i')
bysort year: egen paasche_`i'=sum(infl*s_spending_`i')

* Tornqvist
gen weight_`i'=1/2*(s_spending_`i'+so_spending_`i')
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double tornqvist_`i'=exp(numerator_`i')
drop weight* numerator*

* CES
gen double weight_num_`i'=(s_spending_`i'-so_spending_`i')/(ln(s_spending_`i')-ln(so_spending_`i'))
bysort year: egen double weight_den_`i'=sum(weight_num_`i')
gen double weight_`i'=weight_num_`i'/weight_den_`i'
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double ces_`i'=exp(numerator_`i')
drop numerator* weight*

* Express as inflation in % (note that for Tornqvist/CES we need -1 since we build from the price ratio)
replace laspeyres_`i'=laspeyres_`i'*100
replace paasche_`i'=paasche_`i'*100
replace tornqvist_`i'=(tornqvist_`i'-1)*100
replace ces_`i'=(ces_`i'-1)*100
}

keep year las* paa* tor* ces*
duplicates drop
drop if year==2004
collapse (mean) *
drop *_all 
reshape long laspeyres_incq paasche_incq tornqvist_incq ces_incq, i(year) j(income_quintile)
drop year

* Panel A

scatter tornq income_quintile || lfit tornq inc, lc(maroon) graphregion(color(white)) ///
 leg(off) xtitle("Household Income Quintile") ytitle("Tornqvist Annual Inflation Rate," "Avg. 2004-2015, %") ylabel(,angle(0))
graph export "$resultspath/Figure3PanelA.pdf", as(pdf) replace
graph save "$resultspath/Figure3PanelA.gph", replace

* Panel B
keep if income_quintile==1 | income_quintile==5
gsort - income_quintile
foreach i in ces laspeyres paasche tornqvist {
gen `i'_infl_diff=`i'-`i'[_n-1]
tab `i'_infl_diff
}
keep *_infl_diff
rename tornqvist index1
rename laspeyres index2
rename paasche index3
rename ces index4
drop if missing(index1)
gen id=1
reshape long index, i(id) j(price_index)
save "$rootcpi\results", replace

scatter index price_index, ///
xlabel(1 "Tornqvist" 2 "Laspeyres" 3 "Paasche" 4 "CES") ylabel(0.25(0.05)0.45, angle(0)) ///
xtitle("") ytitle("Annual Inflation Difference b/w Bottom" "and Top Income Quintiles, Avg. 2004-2015, pp") ///
graphregion(color(white))
graph export "$resultspath/Figure3PanelB.pdf", as(pdf) replace
graph save "$resultspath/Figure3PanelB.gph", replace

** Panels C **
* Repeat the above just for the goods that are plausibly covered in Nielsen
* there are 91 such product categories, so this is quite similar to "product groups" 

use  "$rootcpi\cpi_cex_2004_2015_final", clear
keep if Sub=="Alcohol_home" | ///
Sub=="Food_home" | ///
Sub=="Smoking_Products" | ///
item=="Stationery, stationery supplies" | ///
item=="Personal care products" | ///
item=="Household cleaning products" | ///
item=="Hair, dental, shaving, and miscellaneous personal care products"

distinct item_name
rename spending spending_all
foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
bysort year: egen double tot_`i'=sum(`i')
gen s_`i'=`i'/tot_`i'
}

egen id=group(series_id)
tsset id year

foreach i in spending_all spending_incq1 spending_incq2 spending_incq3 spending_incq4 spending_incq5 {
gen so_`i'=L.s_`i'
}

* Compute price indices
gen price_ratio=infl+1

foreach i in all incq1 incq2 incq3 incq4 incq5 {

* Laspeyres & paasche
bysort year: egen laspeyres_`i'=sum(infl*so_spending_`i')
bysort year: egen paasche_`i'=sum(infl*s_spending_`i')

* Tornqvist
gen weight_`i'=1/2*(s_spending_`i'+so_spending_`i')
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double tornqvist_`i'=exp(numerator_`i')
drop weight* numerator*

* CES
gen double weight_num_`i'=(s_spending_`i'-so_spending_`i')/(ln(s_spending_`i')-ln(so_spending_`i'))
bysort year: egen double weight_den_`i'=sum(weight_num_`i')
gen double weight_`i'=weight_num_`i'/weight_den_`i'
gen double numerator_temp_`i'=weight_`i'*log(price_ratio)
bysort year: egen double numerator_`i'=sum(numerator_temp_`i')
gen double ces_`i'=exp(numerator_`i')
drop numerator* weight*

* Express as inflation in % (note that for Tornqvist/CES we need -1 since we build from the price ratio)
replace laspeyres_`i'=laspeyres_`i'*100
replace paasche_`i'=paasche_`i'*100
replace tornqvist_`i'=(tornqvist_`i'-1)*100
replace ces_`i'=(ces_`i'-1)*100
}

keep year las* paa* tor* ces*
duplicates drop
drop if year==2004
collapse (mean) *
drop *_all 
reshape long laspeyres_incq paasche_incq tornqvist_incq ces_incq, i(year) j(income_quintile)
drop year

* Panel C
keep if income_quintile==1 | income_quintile==5
gsort - income_quintile
foreach i in ces laspeyres paasche tornqvist {
gen `i'_infl_diff=`i'-`i'[_n-1]
tab `i'_infl_diff
}
keep *_infl_diff
rename tornqvist index1
rename laspeyres index2
rename paasche index3
rename ces index4
drop if missing(index1)
gen id=1
reshape long index, i(id) j(price_index)

scatter index price_index, ///
xlabel(1 "Tornqvist" 2 "Laspeyres" 3 "Paasche" 4 "CES") ylabel(0.15(0.05)0.35, angle(0)) ///
xtitle("") ytitle("Annual Inflation Difference b/w Bottom" "and Top Income Quintiles, Avg. 2004-2015, pp") ///
graphregion(color(white))
graph export "$resultspath/Figure3PanelC.pdf", as(pdf) replace
graph save "$resultspath/Figure3PanelC.gph", replace
